package baiqitun.stupro.security.service.impl;

import baiqitun.stupro.common.constant.SysConstant;
import baiqitun.stupro.common.exception.enums.support.ExistedExceptionEnum;
import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.ExistedException;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.model.TreeModel;
import baiqitun.stupro.common.model.vo.TreeNodeVo;
import baiqitun.stupro.common.service.TreeNodeService;
import baiqitun.stupro.common.service.TreeNodeVoService;
import baiqitun.stupro.common.service.support.MyServiceImpl;
import baiqitun.stupro.common.service.support.TreeNodeServiceImpl;
import baiqitun.stupro.security.entity.SysDepart;
import baiqitun.stupro.security.mapper.SysDepartMapper;
import baiqitun.stupro.security.service.SysDepartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author kedaji
 * @since 2020-03-25
 */
@Service
public class SysDepartServiceImpl extends TreeNodeServiceImpl<SysDepartMapper, SysDepart> implements SysDepartService{

    @CachePut("depart")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(@Validated SysDepart depart) {
        checkExistedByName(depart.getName());
        checkNotFound(depart.getParentId(), new NotFoundException(NotFoundExceptionEnum.DEPART_NOT_FOUND));
        return super.save(depart);
    }

    @CachePut("depart")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRoot(@Validated SysDepart depart) {
        checkExistedByName(depart.getName());
        super.save(depart);
    }

    @Caching(evict = @CacheEvict("depart"), put = @CachePut("depart"))
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(@Validated SysDepart depart){
        checkNotFound(depart.getId(), new NotFoundException(NotFoundExceptionEnum.DEPART_NOT_FOUND));
        return super.updateById(depart);
    }

    @CacheEvict("depart")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeById(String id, @NotBlank(message = SysConstant.ER_UPDATE_BY_CANNOT_NULL) String updateBy) {
        checkNotFound(id, new NotFoundException(NotFoundExceptionEnum.DEPART_NOT_FOUND));
        SysDepart depart = new SysDepart();
        depart.setUpdateBy(updateBy);
        updateById(depart);
        return super.removeById(id);
    }

    private void checkExistedByName(String name){
        SysDepart queryDepart = new SysDepart();
        queryDepart.setName(name);
        int count = count(new QueryWrapper<>(queryDepart));
        if (count > 0){
            throw new ExistedException(ExistedExceptionEnum.DEPART_EXISTED).put(SysDepart.class,"name", name);
        }
    }
}
